#!/usr/bin/env python
# -*-coding: utf-8-*-
# AUTHOR ：Arthur
# DATE   ：2016-10-19
# INTRO  ：
# VERSION：0.1


import logging as log
import requests
import config,rule
log.basicConfig(filename='logger.log',level=log.INFO)


class Proxy(object):


    @staticmethod
    def _get_page(url):
        """
        :return: str的网页页面
        """
        # 要爬取的url
        headers = {"User-Agent": config.USER_AGENT}
        try:
            r = requests.get(url, headers=headers)
            if r.status_code == 200:
                print("获取{}页面成功".format(url))
                return r.text
        except Exception as e:
            return None

    @staticmethod
    def get_xici_proxies():
        """
        获取西刺代理页面的ip
        :return: ip列表
        """
        url = config.URLS["xici"]
        # 分析页面，返回ip:port列表
        page = Proxy._get_page(url)
        proxies = rule.Rule.xici(page)
        return proxies
    @staticmethod
    def get_daxiang_proxies():
        url = config.URLS["daxiang"]
        page = Proxy._get_page(url)
        proxies = rule.Rule.daixiang(page)
        return proxies
